dofile (getScriptPath() .. "\\..\\processor\\processor.lua")
dofile (getScriptPath() .. "\\..\\processor\\log.lua")
dofile (getScriptPath() .. "\\..\\patterns\\ticks.lua")
dofile (getScriptPath() .. "\\..\\lib\\lib.lua")

ticks.no_load = true

processor:AddInit(function()
	script_name = string.gsub(string.gsub(debug.getinfo(1).short_src, "%.[^%.]*$", ""), "^.*[\\/]", "")
	script_id = script_name
	log_file = lib.path.log .. script_id .. ".log"
	
	processor:InitLog(log_file)
	
	ticks.test_params = lib:LoadIni(lib.path.init .. "test.ini", "ticks")
	if ticks.test_params
		and (ticks.test_params.sec_codes_file
			or ticks.test_params.sec_codes
		)
	then
		if not ticks.test_params.date then
			ticks.test_params.date = lib:FormatDate()
		end
		lib.tick_date = {
			year = int(string.sub(ticks.test_params.date, 1, 4)),
			month = int(string.sub(ticks.test_params.date, 5, 6)),
			day = int(string.sub(ticks.test_params.date, 7, 8))
		}
		if not ticks.test_params.sec_codes then
			ticks.test_params.sec_codes = {}
		end
		if ticks.test_params.sec_codes_file then
			local list = lib:LoadQuotes(lib.path.init .. ticks.test_params.sec_codes_file)
			if list then
				for i, v in ipairs(list) do
					table.sinsert(ticks.test_params.sec_codes, v.sec_code)
				end
			end
		end
		ticks.test_params.index = 0
		ticks.test_params.notify = {}
		message("Test of " .. #ticks.test_params.sec_codes)
		ticks.test_params.start_time = lib:tickseconds()
	else
		message("No test params")
		processor:stop(3)
	end
end)

processor:AddTimer(function(t)
	if #ticks.test_params.sec_codes < ticks.test_params.index then
		processor:stop(3)
		return
	end
	ticks.test_params.index = ticks.test_params.index + 1
	if #ticks.test_params.sec_codes < ticks.test_params.index then
		message(tostring(#ticks.test_params.notify))
		if #ticks.test_params.notify > 0 then
			table.sort(ticks.test_params.notify,
				function(small, big)
					return small.seconds__ < big.seconds__
				end
			)
		
			for i, v in ipairs(ticks.test_params.notify) do
				pattern.ticks:SaveLabel(v)
			end
		end
	
		message("Test duration " .. lib:FormatTime(lib:tickseconds() - ticks.test_params.start_time))
		return
	end
	ticks:load2queue(ticks.test_params.sec_codes[ticks.test_params.index], ticks.test_params.date)
	local n = 0
	
	local price, datetime = ticks.tick()
	
	while price do
		datetime.seconds__ = lib:tickseconds(datetime)
		datetime.sec_code__ = ticks.test_params.sec_codes[ticks.test_params.index]
		if not datetime.price__ then datetime.price__ = price end
		table.insert(ticks.test_params.notify, datetime)
		n = n + 1
		price, datetime = ticks.tick()
	end
	
	if n > 0 then message("Notifies saved " .. n) end
end)

processor:AddTimer(function()
	ticks.data = {}
		
	collectgarbage("collect")
end)
